package tech.springboot.springtasks.schedule;

import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.concurrent.CompletableFuture;

/**
 * @author pdai
 */
@Slf4j
@Component
public class AsyncDemo {

    @Async
    public void test(){
        System.out.println(Thread.currentThread().getName() + "---> test...");
    }

    @Async("threadPoolTaskExecutor")
    public void test2(){
        System.out.println(Thread.currentThread().getName() + "---> test...");
    }

    @Async
    public CompletableFuture<String> performLongRunningTask() {

        System.out.println("Starting long running task on thread: " + Thread.currentThread().getName());
        try {

            Thread.sleep(5000); // 模拟耗时操作
        } catch (InterruptedException e) {

            Thread.currentThread().interrupt();
        }
        System.out.println("Completed long running task on thread: " + Thread.currentThread().getName());
        return CompletableFuture.completedFuture("Result of the task");
    }
}
